
package com.joysuch.wwyt.core.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.joysuch.wwyt.audit.service.AuditLogActionDefService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseMenuFunction;
import com.joysuch.wwyt.core.entity.BaseRoleMenuFunction;
import com.joysuch.wwyt.core.repository.BaseMenuFunctionDao;
import com.joysuch.wwyt.core.repository.BaseRoleMenuFunctionDao;
import com.joysuch.wwyt.dataimport.util.DataImportUtils;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller("test2")
@RequestMapping("/test2")
public class AuthorityController {

    @Autowired
    private BaseMenuFunctionDao menuFunctionDao;
    @Autowired
    private BaseRoleMenuFunctionDao roleMenuFunctionDao;
    @Autowired
    private AuditLogActionDefService auditLogActionDefService;

    @RequestMapping("import-authority")
    @ResponseBody
    public ResultBean importAuthority(MultipartFile file, @RequestParam(defaultValue = "0") int sheet,
            List<String> tenants) {
        try {
            ImportParams params = new ImportParams();
            params.setStartSheetIndex(sheet);
            // params.setNeedSave(true);
            List<AuthoriImportTestBean> result = DataImportUtils.parseData(file.getInputStream(), sheet, 1,
                    AuthoriImportTestBean.class, true);
            Map<String, AuthoriImportTestBean> nameMap = new HashMap<>();
            for (AuthoriImportTestBean b : result) {
                nameMap.put(b.getName(), b);
                if (b.getParentName() == null) {
                    b.setDepth(1);
                }
                if (!"button".equals(b.getType())) {
                    b.setType("menu");
                }
            }
            for (AuthoriImportTestBean b : result) {
                if (b.getParentName() != null) {
                    AuthoriImportTestBean parent = nameMap.get(b.getParentName());
                    if (parent != null) {
                        b.setDepth(parent.getDepth() + 1);
                        if (!"button".equals(b.getType())) {
                            parent.setType("folder");
                        }
                    } else {
                        throw new IllegalStateException(b.getName() + "未找到父节点");
                    }
                }
            }
            for (AuthoriImportTestBean b : result) {
                BaseMenuFunction f = new BaseMenuFunction();
                f.setName(b.getName());
                f.setDepth(b.getDepth());
                f.setPerm(b.getPerm());
                f.setType(b.getType());
                f.setUrl(b.getPath());
                menuFunctionDao.save(f);
                BaseRoleMenuFunction bmf = new BaseRoleMenuFunction();
                bmf.setRoleId(1L);
                bmf.setMenuId(f.getId());
                b.setId(f.getId());
                roleMenuFunctionDao.save(bmf);
                b.setEntity(f);
            }
            for (AuthoriImportTestBean b : result) {
                AuthoriImportTestBean parent = nameMap.get(b.getParentName());
                if (parent != null) {
                    b.getEntity().setParent(menuFunctionDao.getOne(parent.getId()));
                    menuFunctionDao.save(b.getEntity());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBean.success("导入成功");
    }

    @RequestMapping("reload-audit-action-def")
    @ResponseBody
    public ResultBean reload() {
        auditLogActionDefService.reload();
        return ResultBean.success("成功");
    }

    @RequestMapping("import-authority-using-exists")
    @ResponseBody
    public ResultBean importAuthorityUsingExists(MultipartFile file, @RequestParam(defaultValue = "0") int sheet) {
        try {
            ImportParams params = new ImportParams();
            params.setStartSheetIndex(sheet);
            // params.setNeedSave(true);
            List<AuthoriImportTestBean> result = DataImportUtils.parseData(file.getInputStream(), sheet, 1,
                    AuthoriImportTestBean.class, true);
            for (AuthoriImportTestBean b : result) {
                if ("menu".equals(b.getType())) {
                    BaseMenuFunction menu = menuFunctionDao.findByName(b.getName());
                    if (menu != null) {
                        continue;
                    }
                }
                if (b.getParentName() != null) {
                    BaseMenuFunction parent = menuFunctionDao.findByName(b.getParentName());
                    Assert.notNull(parent, String.format("第%d行,父菜单[%s]不存在", b.getRowIndex(), b.getParentName()));
                    BaseMenuFunction f = new BaseMenuFunction();
                    f.setName(b.getName());
                    f.setDepth(parent.getDepth() + 1);
                    f.setPerm(b.getPerm());
                    f.setType(b.getType());
                    if ("menu".equals(b.getType()) || "folder".equals(b.getType())) {
                        if (b.getPath().startsWith("/")) {
                            f.setUrl(b.getPath());
                        } else {
                            f.setUrl(parent.getUrl() + "/" + b.getPath());
                        }
                    }
                    f.setParent(parent);
                    f.setMenuOrder(b.getOrderNo());
                    menuFunctionDao.save(f);
                } else {
                    BaseMenuFunction f = new BaseMenuFunction();
                    f.setName(b.getName());
                    f.setDepth(1);
                    f.setPerm(b.getPerm());
                    f.setType(b.getType());
                    f.setMenuOrder(b.getOrderNo());
                    f.setUrl(b.getPath());
                    menuFunctionDao.save(f);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalStateException(e.getMessage(), e);
        }
        return ResultBean.success("导入成功");
    }
}
